//
//  StatusCell.swift
//  WeiBo_Private
//
//  Created by Yangperlove on 16/1/17.
//  Copyright © 2016年 Yangperlove. All rights reserved.
//

import UIKit
import SnapKit

let statusCellBottomViewHeight: CGFloat = 35

class StatusCell: UITableViewCell {
    
    var status: Status? {
        didSet {
               statusOriginalView.statusModel = status
               topConstraint?.uninstall()
            
            if let re = status?.retweeted_status {
                //转发微博
                
                //1.设置数据源
                statusCellRetweetedView.retweeted_status = re
                //2.显示转发微博视图
                statusCellRetweetedView.hidden = false
                //3.更新约束 相对于转发微博的底部 + 10
                statusCellBottomView.snp_updateConstraints(closure: { (make) -> Void in
                    self.topConstraint = make.top.equalTo(statusCellRetweetedView.snp_bottom).constraint
                })
            } else {
                //1.隐藏转发微博视图
                statusCellRetweetedView.hidden = true
                //2.更新约束 相对原创微博的底部 + 10
                statusCellBottomView.snp_updateConstraints(closure: { (make) -> Void in
                    self.topConstraint = make.top.equalTo(statusOriginalView.snp_bottom).constraint
                })
            }

//                guard let image = status?.retweeted_status else {
//                    //2.显示转发微博视图
//                    statusCellRetweetedView.hidden = true
//                    statusCellBottomView.snp_updateConstraints(closure: { (make) -> Void in
//                        self.topConstraint = make.top.equalTo(statusOriginalView.snp_bottom).constraint
//                    })
//                    return
//                }
//            //2.显示转发微博视图
//            statusCellRetweetedView.hidden = false
//                statusCellRetweetedView.retweeted_status = image
//                statusCellBottomView.snp_updateConstraints { (make) -> Void in
//                    self.topConstraint = make.top.equalTo(statusCellRetweetedView.snp_bottom).offset(kStatusCellMargin).constraint
//                }
        }
    }

    
    var topConstraint: Constraint?

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
        
    }
    
    func setupUI() {
        contentView.addSubview(statusOriginalView)
        contentView.addSubview(statusCellRetweetedView)
        contentView.addSubview(statusCellBottomView)
        addSubview(seperateView)
        
        statusOriginalView.snp_makeConstraints { (make) -> Void in
            make.top.right.left.equalTo(contentView)
        }
        
        statusCellRetweetedView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(statusOriginalView.snp_bottom)
            make.left.equalTo(statusOriginalView.snp_left).offset(kStatusCellMargin)
            make.right.equalTo(statusOriginalView.snp_right).offset(-kStatusCellMargin)
        }
        
        statusCellBottomView.snp_makeConstraints { (make) -> Void in
            make.left.right.equalTo(contentView)
            topConstraint = make.top.equalTo(statusCellRetweetedView.snp_bottom).offset( kStatusCellMargin).constraint
            make.height.equalTo(statusCellBottomViewHeight)
        }
        
        seperateView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(statusCellBottomView.snp_bottom)
            make.left.right.equalTo(statusCellBottomView)
            make.height.equalTo(kStatusCellMargin)
        }
        
        //MARK:/*一定不要忘了这一步,通过设置CONTNETVIEW底部的约束可以自动计算行高*/
        contentView.snp_makeConstraints { (make) -> Void in
            make.top.left.right.equalTo(self)
            make.bottom.equalTo(statusCellBottomView.snp_bottom).offset(kStatusCellMargin)
        }
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    lazy var statusOriginalView: StatusOriginalView = StatusOriginalView()
    lazy var statusCellBottomView: StatusCellBottomView = StatusCellBottomView()
    lazy var statusCellRetweetedView: StatusCellRetweetedView = StatusCellRetweetedView()
    lazy var seperateView: UIView = {
       let sepView = UIView()
        sepView.backgroundColor = UIColor(white: 0.8, alpha: 1)
        
        return sepView
    }()
}
